package cn.tedu.tmall.admin.mall.dao.repository.impl;

import cn.tedu.tmall.admin.mall.dao.mapper.GoodsDetailMapper;
import cn.tedu.tmall.admin.mall.dao.mapper.GoodsMapper;
import cn.tedu.tmall.admin.mall.dao.repository.IGoodsRepository;
import cn.tedu.tmall.admin.mall.pojo.po.GoodsDetailPO;
import cn.tedu.tmall.admin.mall.pojo.po.GoodsPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class GoodsRepository implements IGoodsRepository {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsDetailMapper goodsDetailMapper;

    @Override
    public Integer countGoodsByTitle(String title) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", title);
        return goodsMapper.selectCount(queryWrapper);
    }

    @Override
    public Integer saveGoods(GoodsPO goodsPO) {
        return goodsMapper.insert(goodsPO);
    }

    @Override
    public Integer saveGoodsDetail(GoodsDetailPO goodsDetailPO) {
        return goodsDetailMapper.insert(goodsDetailPO);
    }

    @Override
    public GoodsPO basic(Long goodsId) {
        return goodsMapper.selectById(goodsId);
    }

    @Override
    public GoodsDetailPO detail(Long goodsId) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",goodsId );
        return goodsDetailMapper.selectOne(queryWrapper);
    }

    @Override
    public List<GoodsPO> listGoods(Long categoryId, Integer pageNum, Integer pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("category_id",categoryId);
        PageHelper.startPage(pageNum,pageSize);
        List list = goodsMapper.selectList(queryWrapper);
        return list;
    }
}
